<?php

class TestController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        Zend_Loader::loadClass('Test');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Article');
        Zend_Loader::loadClass('Phrase');
    }
    
    function preDispatch()
    {
        $Article = new Article();
        $this->view->main_menu = $Article->getPublished();
        
        $Phrase = new Phrase();
        $this->view->phrase = $Phrase->getRandom();
        
        $this->view->title = 'Test';
    }
        
    function indexAction()
    {
        $this->view->pageInfo = new stdClass();
        $this->view->pageInfo->alias = 'test';
        
        $this->view->new_id = 0;
        $this->view->answers = array();
        $this->view->level = $this->_request->getParam('level');
        
        Zend_Session::start();
        
        if(in_array($this->view->level,array(1,2,3))){
            $test = new Test();
            $this->view->list = $test->fetchAll('complexity='.(int)$this->view->level,'id');
        }
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $params['authgroup_id'] = 1; //student
            $params['first_name'] = trim($filter->filter($this->_request->getPost('first_name')));
            $params['last_name'] = trim($filter->filter($this->_request->getPost('last_name')));
            $params['email'] = trim($filter->filter($this->_request->getPost('email')));
            $params['phone'] = trim($filter->filter($this->_request->getPost('phone')));
            $params['register_date'] = date('Y-m-d H:i:s');
            $params['address'] = trim($filter->filter($this->_request->getPost('address')));
            $params['contacts'] = trim($filter->filter($this->_request->getPost('contacts')));
            $params['email'] = trim($filter->filter($this->_request->getPost('email')));
            $params['birthday'] = trim($filter->filter($this->_request->getPost('birthday')));
            
            $this->view->answers = $this->_request->getPost('questions');
            
            $obj = new User();
            $exist = $obj->getUserByMail($params['email']);
            $this->view->message = $obj->validate(0,$params,false);
            if($this->view->message===true) {
                if(!$exist) $this->view->new_id = $obj->insert($params);
                else $this->view->new_id = $exist;
                if($this->view->new_id>0) {
                    $this->view->result = '';
                    
                    $admin_part = '';
                    foreach($params as $key=>$value) {
                        $admin_part .= $key.' : '.$value.'<br>';
                    }
                    
                    $this->view->result .= '<br>Result<br><br><table cellspacing="5">';
                    $this->view->result .= '<tr>';
                    $this->view->result .= '<th align="left">Question</th>';
                    $this->view->result .= '<th align="left">Answer</th>';
                    $this->view->result .= '<th>Result</th>';
                    $this->view->result .= '</tr>';
                    foreach($this->view->list as $question) {
                        $this->view->result .= '<tr>';
                        $this->view->result .= '<td align="left">'.$question->question.' './*$question->correct.*/'</td>';
                        $this->view->result .= '<td align="left">'.$this->view->answers[$question->id].'</td>';
                        $this->view->result .= '<td>'.($this->view->answers[$question->id]==$question->correct?'<font color="green"><b>Correct</b></font>':'<font color="red"><b>Wrong</b></font>').'</td>';
                        $this->view->result .= '</tr>';
                    }
                    $this->view->result .= '</table>';
                    //echo $admin_part.$this->view->result.'<br>';
                    
                    $headers  = 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
                    $headers .= 'From: simplexlingua.net <no-reply@simplexlingua.net>' . "\r\n";
                    
                    $result = mail(ADMIN_EMAIL,'Test '.$this->view->level,$admin_part.$this->view->result,$headers);
                    //var_dump($result);
                    $result = mail($params['email'],'Test '.$this->view->level,$this->view->result,$headers);
                    //var_dump($result);
                    //exit();
                    
                    $this->_redirect('/page/thankyou.html'); 
                }
            }
            
        }
        
        $this->view->first_name = isset($params['first_name'])?$params['first_name']:'';
        $this->view->last_name = isset($params['last_name'])?$params['last_name']:'';
        $this->view->email = isset($params['email'])?$params['email']:'';
        $this->view->phone = isset($params['phone'])?$params['phone']:'';
        $this->view->address = isset($params['address'])?$params['address']:'';
        $this->view->contacts = isset($params['contacts'])?$params['contacts']:'';
        $this->view->birthday = isset($params['birthday'])?$params['birthday']:'';
        
        $this->render();        
    }
    
}